<template>
  <div class="goods">
      <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form>
      <el-form-item style="margin-top: 22px;">
        <el-input style="width:350px"
         placeholder="请输入内容"
         clearable
         @keyup.enter.native="getGoodsList"
         v-model="query">
          <template slot="append">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </template>
        </el-input>
        <el-button type="primary" @click="addGoods">添加商品</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="goodsTable" border  max-height="500" style="width: 100%;margin-bottom: 15px;">
      <template slot="empty">
        暂无数据
      </template>
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column prop="goods_name" label="商品名称"></el-table-column>
      <el-table-column prop="goods_price" label="商品价格（元）"></el-table-column>
      <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
      <el-table-column prop="add_time" label="创建日期">
          <template slot-scope="scope">
              {{scope.row.add_time | YYYYMMDD}}
          </template>
      </el-table-column>
      <el-table-column label="操作"></el-table-column>
    </el-table>
    <el-pagination
      :current-page="pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="sizeChange"
      @current-change="currentChange"
    ></el-pagination>
  </div>
</template>

<script type="text/ecmascript-6">
export default {
  data() {
    return {
        goodsTable:[],
        query: '',
        pagenum: 1,
        pagesize: 10,
        total: 0
    }
  },
  methods:{
      //获取商品列表
      getGoodsList() {
          this.$http('goods', {}, 'get', {
              query: this.query,
              pagenum: this.pagenum,
              pagesize: this.pagesize
          })
          .then(res => {
              this.goodsTable = res.data.goods
              this.total = res.data.total
          })
      },
      sizeChange(val) {
          this.pagenum = 1
          this.pagesize = val
          this.getGoodsList()
      },
      currentChange(val) {
          this.pagenum = val
          this.getGoodsList()
      },
      //添加商品
      addGoods(){
          this.$router.push({
              name: 'addGoods'
          })
      }
  },
  created(){
      this.getGoodsList()
  }
}
</script>

<style scoped lang="scss">
.goods {
  background: #fff;
  border-radius: 5px;
  padding: 15px;
}
</style>
